# -*- coding: utf-8 -*-

import datetime

from peewee import *
from base_model import BaseModel as CrudModel
from mocrud.admin import ModelAdmin


class Category(CrudModel):
    name = CharField(verbose_name=u'类别名称')
    description = TextField(null=True, verbose_name=u'描述')
    def __unicode__(self):
        return self.name
  
class CategoryAdmin(ModelAdmin):
    verbose_name = u'书籍分类'
Category.Admin = CategoryAdmin


class Reader(CrudModel):
    name = CharField(verbose_name=u'姓名')
    status = IntegerField(choices=((0, u'正常'), (1, u'禁用')), null=True, default=0, verbose_name=u'状态')
    
    def __unicode__(self):
        return self.name
    
    def read_count(self):
        return self.reads.count()
    
    def status_dis(self):
        if self.status==0:
            return u'正常'
        else:
            return u'禁用'
        
    def borrowed_count(self):
        return self.reads.where(BorrowRecord.revert_flag==False).count()
    
class ReaderAdmin(ModelAdmin):
    verbose_name = u'读者'
    columns = ('name', 'status_dis', 'read_count', 'borrowed_count')
    add_column_display = {
                          'status_dis': u'状态',
                          'read_count': u'累计借书量',
                          'borrowed_count': u'未还 /本',
    }
    #show_nav = False
Reader.Admin = ReaderAdmin
    


class Book(CrudModel):

    seq = CharField(verbose_name=u'序列号')
    title = CharField(max_length=511, verbose_name=u'书名')
    
    ISBN = CharField(max_length=13, null=True, verbose_name=u'国际标准书号')
    url = CharField(max_length=4095, null=True)
    author = CharField(null=True, verbose_name=u'作者')
    genre = CharField(null=True, verbose_name=u'出版社')
    editor = CharField(null=True, verbose_name=u'编辑')

    description = TextField(null=True, verbose_name=u'描述')
    thumbnail = TextField(null=True, verbose_name=u'图片')

    purchaseDate = DateField(default=datetime.datetime.now, null=True, verbose_name=u'采购日期')
    purchaseValue = DecimalField(null=True, max_digits=10, decimal_places=2)
    
    owned = BooleanField(default=False, verbose_name=u'是否已借')
    owned_count = IntegerField(null=True, default=0, verbose_name=u'借阅次数')
    category = ForeignKeyField(Category, null=True,verbose_name = u'类别')

    status = IntegerField(null=True, default=0, verbose_name=u'状态')
    
    def __unicode__(self):
        return u'%s (%s)'%(self.title,self.seq)
    
    def owned_dis(self):
        if self.owned:
            br = self.owners.where(BorrowRecord.revert_flag==False).first()
            return u'是 (%s %s 归还)'%(br.owner,br.plan_time)
        else:
            return u'否'

class BookAdmin(ModelAdmin):
    verbose_name = u'图书'
    columns = ('title', 'author', 'purchaseDate','seq', 'category', 'owned_count', 'owned_dis')
    exclude = ('ISBN','url','description','thumbnail','purchaseValue','owned','owned_count','status')
    add_column_display = {
                          'owned_dis': u'是否已借',
    }
Book.Admin = BookAdmin


class BorrowRecord(CrudModel):
    owner = ForeignKeyField(Reader,verbose_name = u'借书人',related_name='reads')
    book = ForeignKeyField(Book,verbose_name = u'所借书',related_name='owners')
    revert_flag = BooleanField(default=False, verbose_name=u'是否已还')
    owned_time = DateTimeField(default=datetime.datetime.now,verbose_name=u'借书时间')
    plan_time = DateField(default=datetime.datetime.now, verbose_name=u'归还日期')
    revert_time = DateTimeField(null=True,default=datetime.datetime.now, verbose_name=u'实际归还时间')
    
#BorrowRecord.drop_table()
    
class BorrowRecordAdmin(ModelAdmin):
    verbose_name = u'借阅记录'
    foreign_key_lookups = {'book': 'title','owner': 'name'}
BorrowRecord.Admin = BorrowRecordAdmin


class BookForBorrow(Book):
    
    def __unicode__(self):
        return u'%s'%self.title
    
    class Meta:
        db_table = 'book'

class BookForBorrowAdmin(ModelAdmin):
    verbose_name = u'图书借阅'
    visible = False
    columns = ('title', 'author','genre','category','description','purchaseDate','owned_dis')
    filter_fields = ('title','seq', 'author','genre', 'category', 'editor','description','owned')
    add_column_display = {
                          'owned_dis': u'是否已借',
    }
BookForBorrow.Admin = BookForBorrowAdmin